<template>
	<view class="agent-page">
		<u-navbar :autoBack="true" title="代理认证" :placeholder="true" bgColor="rgba(255,255,255,0)"></u-navbar>
		<view v-if="status == null">
			<view class="content">
				<view class="head">
					<view class="head-left">
						<view class="head-title">
							认证代理
						</view>
						<view class="head-text">
							更加真实，安全保障
						</view>
					</view>
					<image src="https://m.lqvvvv.com/assets/static/my/agent.png" mode=""></image>
				</view>
				<view class="info-box">
					<view class="info-title">
						请上传法人身份证的正反面
					</view>
					<view class="upload-item" @click="upfun(1)">
						<view class="upload-left">
							<view class="upload-title">
								头像面
							</view>
							<view class="upload-text">
								上传身份证头像面
							</view>
						</view>
						<image v-if="positive == ''" src="https://m.lqvvvv.com/assets/static/my/human.png" mode="">
						</image>
						<image v-else :src="'https://m.lqvvvv.com'+positive" mode=""></image>
					</view>
					<view class="upload-item" @click="upfun(2)">
						<view class="upload-left">
							<view class="upload-title">
								国徽面
							</view>
							<view class="upload-text">
								上传身份证国徽面
							</view>
						</view>
						<image v-if="side == ''" src="https://m.lqvvvv.com/assets/static/my/national.png" mode="">
						</image>
						<image v-else :src="'https://m.lqvvvv.com'+side" mode=""></image>
					</view>
					<view class="demand">
						<view class="demand-title">
							拍摄要求
						</view>
						<view class="demand-text">
							大陆持有本人有效二代身份证
						</view>
						<view class="demand-text">
							拍摄时确保身份正边框完整，字迹清晰，亮度均匀
						</view>
						<image src="https://m.lqvvvv.com/assets/static/my/demand.png" mode=""></image>
					</view>
					<view class="agreement">
						<u-checkbox-group>
							<u-checkbox @change="handleChange" :value="checked" size="16" active-color="#5755F2"
								shape="circle"></u-checkbox>
						</u-checkbox-group>
						<view class="agreement-text">
							去签订<text @click="goagreement('shop_agreement')">《商家协议》</text>
						</view>
					</view>
					<view class="next-btn">
						<u-button size="large" shape="circle" color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)"
							type="primary" text="提交" @click="identify"></u-button>
					</view>
				</view>
			</view>
		</view>
		<view v-else> 
			<view class="content">
				<view class="icon" v-if="status.approve_status == 2">
					<image src="https://m.lqvvvv.com/assets/static/my/no.png" mode=""></image>
					<view class="title">审核失败</view>
				</view>
				<view class="icon3" v-if="status.approve_status == 1">
					<image src="https://m.lqvvvv.com/assets/static/my/scess.png" mode=""></image>
					<view class="title">审核已通过</view>
				</view>
				<view class="icon2" v-if="status.approve_status == 0">
					<image src="https://m.lqvvvv.com/assets/static/my/tisp.png" mode=""></image>
					<view class="title">请等待...</view>
				</view>
				<view style="margin-top: 200rpx;">
					<u-button size="large" shape="circle" color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)"
						type="primary" v-if="status.approve_status == 0" text="审核中..."></u-button>
					<u-button size="large" @click="$navTo.togo('/pages/my/agent/agency-pay/index')" shape="circle"
						color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)" type="primary"
						v-if="status.approve_status == 1" text="前往选购代理"></u-button>
					<u-button size="large" shape="circle" color="linear-gradient( 90deg, #C58AF0 0%, #6272ED 100%)"
						type="primary" v-if="status.approve_status == 2" @click="gonewup" text="已拒绝,重新提交"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: false,
				positive: '',
				side: '',
				license: '',
				status: null
			}
		},
		onLoad() {
			this.getUserAgent()
		},
		methods: {
			gonewup() {
				this.status = null
			},
			getUserAgent() {
				this.$Request({
					method: 'POST',
					url: '/api/user/getUserAgent',
					data: {},
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.status = res.data
						console.log(this.status)
					}
				})
			},
			handleChange() {
				this.checked = !this.checked
			},
			goagreement(name) {
				this.$navTo.togo('/pages/my/agreement/index', {
					name: name
				})
			},
			upfun(type) {
				const app = this
				uni.chooseImage({
					count: 1, //默认1
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: async function(res) {
						uni.showLoading({
							title: '上传中...',
							mask: true
						})
						console.log(res)
						const result = await app.uploadFilePromise(res.tempFilePaths[0])
						uni.hideLoading()
						if (type == 1) {
							app.positive = result
						}
						if (type == 2) {
							app.side = result
						}
						if (type == 3) {
							app.license = result
						}
					}
				});
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://m.lqvvvv.com/api/common/upload',
						filePath: url,
						name: 'file',
						header: {
							token: uni.getStorageSync("token") || '',
						},
						success: (res) => {
							let info = JSON.parse(res.data)
							if (info.code == 1) {
								setTimeout(() => {
									resolve(info.data.url)
								}, 1000)
							} else {
								uni.showToast({
									title: info.msg,
									icon: 'none'
								})
							}
						}
					});
				})
			},
			identify() {
				if (this.positive == '') {
					uni.showToast({
						title: '请上传身份证头像面',
						icon: 'none'
					})
					return
				}
				if (this.side == '') {
					uni.showToast({
						title: '上传身份证国徽面',
						icon: 'none'
					})
					return
				}
				if (this.checked == false) {
					uni.showToast({
						title: '请仔细阅读并同意《商家协议》',
						icon: 'none'
					})
					return
				}
				let params = {
					identity_front: this.positive,
					identity_backend: this.side,
					business_license: this.license
				}
				uni.showLoading({
					mask: true
				})
				this.$Request({
					method: 'POST',
					url: '/api/user/saveUserAgent',
					data: params,
					ismsg: true,
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						this.getUserAgent()
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F7F8FC;
	}
</style>
<style lang="scss" scoped>
	.agent-page {
		min-height: 100vh;
		background-image: url('https://m.lqvvvv.com/assets/static/index/nav-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 700rpx;

		.content {
			padding: 30rpx 30rpx 0;

			.icon {
				width: 232rpx;
				height: 249rpx;
				margin: 0 auto;
				margin-top: 160rpx;

				.title {
					text-align: center;
					margin-top: 20rpx;
				}

				image {
					width: 232rpx;
					height: 249rpx;
				}
			}

			.icon2 {
				width: 186rpx;
				height: 199rpx;
				margin: 0 auto;
				margin-top: 160rpx;

				.title {
					text-align: center;
					margin-top: 20rpx;
				}

				image {
					width: 186rpx;
					height: 199rpx;
				}
			}

			.icon3 {
				width: 202rpx;
				height: 202rpx;
				margin: 0 auto;
				margin-top: 160rpx;

				image {
					width: 202rpx;
					height: 202rpx;
				}

				.title {
					text-align: center;
					margin-top: 20rpx;
				}
			}

			.head {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx 30rpx 0;

				.head-left {
					color: #2E314A;

					.head-title {
						font-size: 48rpx;
						font-weight: bold;
					}

					.head-text {
						margin-top: 30rpx;
						font-size: 32rpx;
						font-weight: 600;
					}
				}

				image {
					width: 203rpx;
					height: 203rpx;
				}
			}

			.info-box {
				padding: 30rpx;
				border-radius: 30rpx;
				background-color: #fff;

				.info-title {
					color: #000;
					font-size: 32rpx;
					font-weight: bold;
					padding: 20rpx 0;
				}

				.info-item {
					padding: 20rpx;
					border-radius: 20rpx;
					background-color: #F6F7F9;
					margin-top: 30rpx;
				}

				.next-btn {
					margin-top: 50rpx;
				}

				.upload-item {
					border-radius: 10rpx;
					background-color: #F3F4F7;
					padding: 30rpx 15rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 30rpx;

					.upload-left {
						color: #000000;
						margin-left: 20rpx;

						.upload-title {
							font-size: 32rpx;
							font-weight: bold;
						}

						.upload-text {
							font-size: 24rpx;
							margin-top: 15rpx;
						}
					}

					image {
						width: 351rpx;
						height: 233rpx;
					}
				}

				.demand {
					margin-top: 30rpx;

					.demand-title {
						font-weight: bold;
						font-size: 32rpx;
						color: #000000;
					}

					.demand-text {
						margin-top: 20rpx;
						font-size: 24rpx;
						color: #8B8DA0;
					}

					image {
						margin-top: 20rpx;
						width: 100%;
						height: 191rpx;
					}
				}


				.agreement {
					display: flex;
					align-items: center;
					margin-top: 40rpx;

					.agreement-text {
						color: #000000;
						font-size: 24rpx;
						font-weight: bold;

						text {
							color: #5755F2;
						}
					}
				}
			}
		}
	}
</style>